<?php
// 工具箱控制器
class ToolAction extends MessageAction {
	/**
     * [contactsList description]通讯录列表
     * @return [type] [description]
     */
    public function contactsList(){
    	$data=$this->getAllDepartment();
        $tree=$this->createTreeStuctDpt($data);
        $json=$this->createSelectHtml($tree);
        $this->assign('json',$json);
        $this->display('Tool:contactsList');
    }

    /*
    跳转发送短信模版
     */
    public function massage(){
    	$this->display('Tool:massage');
    }

    /*
    获取部门人员json数据
     */
    public function dptData(){
		$data=$this->getAllEmploye();
		$tree=$this->createTreeStuctDpt($data);
		$json=$this->createJsonString($tree);
		echo '['.$json.']';
	}

	/*
	群发短信
	 */
	public function fasong(){
		
		$employe_name=$this->getOneEmployeName(session('employe_id'));
		$mobile=I('phone_num');
		$content=I('content').'【来自丽丰OA系统】'.$employe_name.'发送';
		if($this->Lock($mobile.$content)==false){
	    	echo '已经发送成功！';
	    }
	    else{
			
			$a = split(",",$mobile);
			$mobileids='';
		    for($i=0;$i<count($a);$i++){
		        $num = substr($a[$i],0,11);
		        $mobileids .=$num.time().',';
		    }
		    $mobileids=substr($mobileids,0,strlen($mobileids)-1); 
		    $msg=$this->sendSMS($mobile,$content,$mobileids);
		    echo $msg;
	    }
	}

	/**
     * [LockMsg description]锁定半小时后重新发送
     * @param [type] $process_num [description]
     */
    public function Lock($mobile) {
        //return false;
        $ip = get_client_ip().$mobile;
        $obj = S($ip);

        if (empty($obj)) {
            $obj = array(
                $ip => array(
                    'state' => 'lock',
                    'count' => 1,
                    'times' => time()
                )
            );
            S($ip, $obj);
            return true;
        }
        else {
                if (time() - $obj[$ip]['times'] >= 30 * 60) {
                    $obj = array(
                        $ip => array(
                            'state' => 'lock',
                            'count' => 1,
                            'times' => time()
                        ) 
                    );
                    S($ip, $obj);
                    return true;
                }
                return false;
        }
    }

}